package cn.yitun.chat.yitunserverrunsingleboot;

import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.ServerConstant;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunserverrunsingleboot.chat.common.box.ConfigBox;
import cn.yitun.chat.yitunserverrunsingleboot.chat.properties.PathProperties;
import cn.yitun.chat.yitunserverrunsingleboot.chat.properties.ServerProperties;
import cn.yitun.chat.yitunserverrunsingleboot.chat.properties.YitunProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class ConfigInitializingBean implements InitializingBean {

    @Resource
    private ServerProperties serverProperties;

    @Resource
    private PathProperties pathProperties;

    @Resource
    private YitunProperties yitunProperties;

    @Override
    public void afterPropertiesSet(){
        /** 服务器启动的端口 **/
        String serverPort = serverProperties.getServerPort() == null ? null : String.valueOf(serverProperties.getServerPort());
        String serverProxy = serverProperties.getServerProxy() == null ? null : String.valueOf(serverProperties.getServerProxy());

        /** 聊天服务启动TCP端口 **/
        String tcpPortValue = serverProperties.getTcpPort() == null ? null : String.valueOf(serverProperties.getTcpPort());
        String tcpProxy = serverProperties.getTcpProxy() == null ? null : String.valueOf(serverProperties.getTcpProxy());

        /** 聊天服务WebSocket端口 **/
        String websocketPortValue = serverProperties.getWebsocketPort() == null ? null : String.valueOf(serverProperties.getWebsocketPort());
        String websocketProxy = serverProperties.getWebsocketProxy() == null ? null : String.valueOf(serverProperties.getWebsocketProxy());

        /** 聊天服务WebSocket路径 **/
        String websocketPath = YitunStringUtil.isEmpty(serverProperties.getWebsocketPath()) ? null : serverProperties.getWebsocketPath();

        /** 服务器启动的端口 **/
        String addressKey = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_ADDRESS.value();
        String address = serverProperties.getServerAddress();

        String category = "";
        String key = "";
        String value = "";

        category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_BASE.value();
//        category = "server.config.base";
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_ADDRESS.value();
//        key = "server.config.address";
        ConfigBox.put(category, key, address);

        key = ServerConstant.ServerAddressOrPort.SERVER_PORT.value();
//        key = "server.port";
        ConfigBox.put(category, key, serverPort);

        /**
         * -----------------------------------------------------------------------------------------------------
         **/
        /**
         * 这里是读取系统配置文件，用于加载系统启动时需要的端口等配置。
         */

        category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_CHAT.value();
//        category = "server.config.chat";

        /** 聊天服务启动TCP端口 **/
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_TCP_PORT.value();
//        key = "server.config.chat.tcp.port";
        ConfigBox.put(category, key, tcpPortValue);

        /** 聊天服务WebSocket端口 **/
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_WEBSOCKET_PORT.value();
//        key = "server.config.chat.websocket.port";
        ConfigBox.put(category, key, websocketPortValue);

        /** 聊天服务WebSocket路径 **/
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_WEBSOCKET_PATH.value();
//        key = "server.config.chat.websocket.path";
        ConfigBox.put(category, key, websocketPath);

        /**
         * 代理服务端相关配置,这些配置是部署到服务器使用代理的时候需要
         */
        /*************** config start ***********************/

        category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_PROXY.value();

        //服务器是否代理
        key = ServerConstant.ServerAddressOrPort.SERVER_MODE.value();
        ConfigBox.put(category, key, YitunProperties.getMode());
        //http代理
        key = ServerConstant.ServerAddressOrPort.SERVER_PROXY.value();
        ConfigBox.put(category, key, serverProxy);
        //tcp代理
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_TCP_PROXY.value();
        ConfigBox.put(category, key, tcpProxy);
        //ws代理
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_CHAT_WEBSOCKET_PROXY.value();
        ConfigBox.put(category, key, websocketProxy);



        /*************** config end ***********************/


        /**
         * 文件服务相关配置,这些配置是用来启动项目的时候需要的配置
         */
        /*************** config start ***********************/
        category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_FILE.value();
//        category = "server.config.file";

        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_FILE_UPLOAD_PATH.value();
//        key = "server.config.file.upload.path";
        value = serverProperties.getFileUploadPath();
        ConfigBox.put(category, key, value);

        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_FILE_REQUEST_PATH.value();
//        key = "server.config.file.request.path";

        value = serverProperties.getFileRequestPath();
        String fileHttp = ("443".equals(serverPort)) ? "https://" : "http://";
        String filePort = ("80".equals(serverPort) || "443".equals(serverPort)) ? "" : (":" + serverPort);
        value = (null == value || value.isEmpty()) ? (fileHttp + address + filePort) : value;

        ConfigBox.put(category, key, value);
        /*************** config end ***********************/

        /**
         * -----------------------------------------------------------------------------------------------------
         **/

        /**
         * 这里的配置是用来通知客户端，相关服务的地址在哪。需要参考config.properties配置文件相应配置
         */
        /*************** path start ***********************/
        String http = ("443".equals(serverPort)) ? "https://" : "http://";
        String p = ("80".equals(serverPort) || "443".equals(serverPort)) ? "" : (":" + serverPort);

        category = ServerConstant.ServerAddressOrPort.CATEGORY_CONFIG_BASE.value();
//        category = "server.path.base";
        key = ServerConstant.ServerAddressOrPort.SERVER_CONFIG_ENABLED.value();
//        key = "server.address.config.enabled";
        value = serverProperties.getAddressConfigEnabled().toString();
        ConfigBox.put(category, key, value);

        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_MAIN.value();
//        category = "server.path.main";
        // 聊天服务地址
        key = ServerConstant.ServerAddressOrPort.SERVER_MAIN_TCP_ADDRESS.value();
//        key = "server.main.tcp.address";
        value = serverProperties.getMainTcpAddress();
        if (YitunStringUtil.isNotEmpty(value)) {
            value = address;
        }
        ConfigBox.put(category, key, value);

        // 聊天服务端口
        key = ServerConstant.ServerAddressOrPort.SERVER_MAIN_TCP_PORT.value();
//        key = "server.main.tcp.port";
        value = serverProperties.getMainTcpPort();
        if (YitunStringUtil.isNotEmpty(value)) {
            value = tcpPortValue;
        }
        ConfigBox.put(category, key, value);

        // #聊天服务的webSocket路径
        // chat.server.websocket.path=ws://192.168.200.1:12020/oim/websocket
        // websocket地址
        key = ServerConstant.ServerAddressOrPort.SERVER_MAIN_WEBSOCKET_ADDRESS.value();
//        key = "server.main.websocket.address";
        value = serverProperties.getMainWebsocketAddress();
        if (YitunStringUtil.isNotEmpty(value)) {
            String protocol = "443".equals(websocketPortValue) ? "wss://" : "ws://";
            String wp = ("80".equals(websocketPortValue) || "443".equals(websocketPortValue)) ? ""
                    : (":" + websocketPortValue);
            value = protocol + address + wp + websocketPath;
        }
        ConfigBox.put(category, key, value);

        // #聊天服务的http地址
        key = ServerConstant.ServerAddressOrPort.SERVER_MAIN_HTTP_ADDRESS.value();
//        key = "server.main.http.address";
        value = serverProperties.getMainHttpAddress();
        if (YitunStringUtil.isNotEmpty(value)) {
            value = http + address + p;
        }
        ConfigBox.put(category, key, value);

        /**
         * api服务地址
         */
        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_API.value();
//        category = "server.path.api";
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_API_HTTP_ADDRESS.value();
//        key = "server.api.http.address";
        value = pathProperties.getApiHttpAddress();
//        if (YitunStringUtil.isNotEmpty(value)) {
//            value = http + address + p;
//        }
        ConfigBox.put(category, key, value);

        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_API_HTTP_PORT.value();
//        key = "server.api.http.address";
        value = pathProperties.getApiHttpPort() == null ? null : String.valueOf(pathProperties.getApiHttpPort());
        ConfigBox.put(category, key, value);


        /************************** 聊天服务 path start ***********************/

        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_CHAT.value();
//        category = "server.path.chat";
        // 聊天服务端口
        //CHAT-tcp端口
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_TCP_PORT.value();
//        key = "chat.server.tcp.port";
        value = pathProperties.getTcpPort() == null ? null : String.valueOf(pathProperties.getTcpPort());
        if (YitunStringUtil.isEmpty(value)) {
            value = tcpPortValue;
        }
        ConfigBox.put(category, key, value);
        //CHAT-tcp地址
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_TCP_ADDRESS.value();
//        key = "chat.server.tcp.address";
        value = pathProperties.getTcpAddress() == null ? null : pathProperties.getTcpAddress();

        ConfigBox.put(category, key, value);

        /**
         * #聊天服务的webSocket路径
         * chat.server.websocket.path=ws://192.168.200.1:12020/oim/websocket
         * CHAT-websocket地址
         */
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_WEBSOCKET_ADDRESS.value();
//        key = "chat.server.websocket.address";
        value = YitunStringUtil.isEmpty(pathProperties.getWebsocketAddress()) ? null : pathProperties.getWebsocketAddress();
//        if (YitunStringUtil.isEmpty(value)) {
//            String protocol = "443".equals(websocketPortValue) ? "wss://" : "ws://";
//            String wp = ("80".equals(websocketPortValue) || "443".equals(websocketPortValue)) ? "" : (":" + websocketPortValue);
//            value = protocol + address + wp + websocketPath;
//        }
        ConfigBox.put(category, key, value);
        // CHAT-websocket端口
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_WEBSOCKET_PORT.value();
//        key = "chat.server.websocket.port";
        value = NumUtil.isInValid(pathProperties.getWebsocketPort()) ? null : String.valueOf(pathProperties.getWebsocketPort());
        ConfigBox.put(category, key, value);
        // CHAT-websocket路由后缀
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_WEBSOCKET_SUFFIX.value();
//        key = "chat.server.websocket.suffix";
        value = YitunStringUtil.isEmpty(pathProperties.getWebsocketSuffix()) ? null : pathProperties.getWebsocketSuffix();
        ConfigBox.put(category, key, value);

        /**
         * #聊天服务的http地址
         * chat.server.http.url=http://192.168.200.1:12000
         * 聊天服务http地址
         */
        // CHAT-http地址
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_HTTP_ADDRESS.value();
//        key = "chat.server.http.address";
        value = YitunStringUtil.isEmpty(pathProperties.getHttpAddress()) ? null : pathProperties.getHttpAddress();
        ConfigBox.put(category, key, value);
        // CHAT-http端口
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_HTTP_PORT.value();
//        key = "chat.server.http.port";
        value = NumUtil.isInValid(pathProperties.getHttpPort()) ? null : String.valueOf(pathProperties.getHttpPort());
        ConfigBox.put(category, key, value);

        /**
         * 文件服务地址
         * file.server.http.url=http://192.168.200.1:12000
         * category = "server.path.file";
         */
        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_FILE.value();
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_FILE_HTTP_ADDRESS.value();
//        key = "chat.server.file.http.address";
        value = YitunStringUtil.isEmpty(pathProperties.getFileHttpAddress()) ? null : pathProperties.getFileHttpAddress();
        ConfigBox.put(category, key, value);

        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_FILE_HTTP_PORT.value();
//        key = "chat.server.file.http.port";
        value = NumUtil.isInValid(pathProperties.getFileHttpPort()) ? null : String.valueOf(pathProperties.getFileHttpPort());
        ConfigBox.put(category, key, value);
        /**
         * 文件服务地址
         * file.server.http.url=http://192.168.200.1:12000
         * category = "server.path.file";
         */
        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_FILE.value();
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_FILE_HTTP_ADDRESS.value();
//        key = "chat.server.file.http.address";
        value = YitunStringUtil.isEmpty(pathProperties.getFileHttpAddress()) ? null : pathProperties.getFileHttpAddress();
        ConfigBox.put(category, key, value);

        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_FILE_HTTP_PORT.value();
//        key = "chat.server.file.http.port";
        value = NumUtil.isInValid(pathProperties.getFileHttpPort()) ? null : String.valueOf(pathProperties.getFileHttpPort());
        ConfigBox.put(category, key, value);

        /**
         * 远程调用地址
         */
        category = ServerConstant.ServerAddressOrPort.CATEGORY_PATH_REMOTE.value();
        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_REMOTE_TCP_ADDRESS.value();
//        key = "chat.server.remote.http.address";
        value = YitunStringUtil.isEmpty(pathProperties.getRemoteTcpAddress()) ? null : pathProperties.getFileHttpAddress();
        ConfigBox.put(category, key, value);

        key = ServerConstant.ServerAddressOrPort.CHAT_SERVER_REMOTE_TCP_PORT.value();
//        key = "chat.server.remote.http.port";
        value = NumUtil.isInValid(pathProperties.getRemoteTcpPort()) ? null : String.valueOf(pathProperties.getRemoteTcpPort());
        ConfigBox.put(category, key, value);

        /************************** 聊天服务 path end ***********************/

    }

}
